<script setup lang="ts">
import { getCurrentInstance, ref, onMounted, onUpdated, nextTick } from 'vue'
import { onBeforeRouteLeave } from 'vue-router'
import MeScroll from 'mescroll.js'
import 'mescroll.js/mescroll.min.css'
const { proxy } = getCurrentInstance()

/* ---- */
const logUser = proxy.$Store.get('logUser') || {}
const WXConfig = proxy.$Store.get('WXConfig') || {}

/* --设置微信分享功能-- */
proxy.$WxTool.loadJsdk(WXConfig, (obj) => {
  if (!obj) return
  // ----
  obj.hideMenuItem()
})

/* ---- */
const user = {
  forAdm: String(proxy.$Phone) === String(logUser.telphone)
}
// ----
const totalCount = ref(0)
// --mescroll--
const noData = proxy.$Global.noData
let mescroll = null
const items = ref([])

/* ---- */
const layer = ref()

/* ---- */
// --getData--
const startMeScroll = () => {
  mescroll = new MeScroll(proxy.$refs.dataScroll, {
    up: {
      callback: getListData,
      isBounce: false,
      page: { num: 0, size: 10 },
      htmlNodata: '<p class="upwarp-nodata">-- 到底啦 --</p>',
      noMoreSize: 10,
      toTop: {
        src: '/pics/mescroll-totop.png',
        offset: 1000
      },
      empty: {
        warpId: proxy.$refs.dataList,
        tip: noData
      }
    }
  })
}
const getListData = (page) => {
  getListDataFromNet({
    forUrl: 'dealer/index',
    forData: {
      params: {
        'DealerSearch[parent_dealer_id]': user.forAdm ? undefined : logUser.id,
        // ----
        sort: 'SORT_DESC',
        // ----
        page: page.num,
        PageSize: page.size
      }
    }
  }, page.num, page.size, (curPageData) => {
    setListData(curPageData).then((result) => {
      mescroll.endSuccess(curPageData ? curPageData.length : 0, true)
      items.value = items.value.concat(result)
    })
  }, (resMsg) => {
    mescroll.endErr()
    proxy.$Myfun.forToast(layer.value, 'open', resMsg)
  })
}
const getListDataFromNet = (option, pageNum, pageSize, successCallback, errorCallback) => {
  if (Number(pageNum) === 1) {
    if (document.querySelector('.upwarp-nodata')) { document.querySelector('.upwarp-nodata').style.visibility = 'hidden' }
    items.value = []
  }
  // ----
  proxy.$http.get(option.forUrl, option.forData).then((res) => {
    totalCount.value = res._meta.totalCount
    // ----
    successCallback && successCallback(res.items)
  }).catch((err) => { errorCallback && errorCallback(err) })
}
const setListData = async (items) => {
  const newItems = []
  for (let i = 0; i < Number(items.length); i++) {
    const item = items[i]
    // ----
    newItems.push({
      id: item.id,
      name: proxy.$Myfun.setText(item.name),
      leader: proxy.$Myfun.setText(item.leader),
      date: proxy.$Myfun.getDateShort(item.created_at),
      source: item.source,
      telphone: item.telphone
    })
  }
  return newItems
}
const divScroll = (event) => { proxy.$Myfun.setImgToTop() }
// ----
const getAuditFun = (type, item) => {
  switch (Number(type)) {
    case 1: {
      window.location.href = 'tel:' + item.telphone
      break
    }
  }
}

/* ---- */
onMounted(() => {
  // --创建MeScroll对象--
  setTimeout(() => { startMeScroll() }, 500)
})
onUpdated(() => { nextTick(() => { proxy.$Myfun.setSectionH() }) })
onBeforeRouteLeave(() => { if (mescroll) mescroll.destroy() })
</script>

<template>
  <div>
    <header>
      <div class="xw-msg xw-bgCL-11"><span>注册商友列表：{{totalCount}}人</span></div>
    </header>
    <div class="xw-section">
      <div ref="dataScroll" class="mescroll" @scroll="divScroll">
        <ul class="list-reg xw-flex xw-flex-column" ref="dataList">
          <li class="xw-bg-white xw-radius-3" v-for="(item, index) in items">
            <div class="LRG-title xw-ellipsis-2" v-if="item.name">{{item.name}}</div>
            <div class="LRG-label xw-flex xw-flex-align">
              <em class="xw-color-13 far fa-clock"></em>
              <div class="xw-flex-W1">{{item.date}}</div>
            </div>
            <div class="LRG-label xw-flex xw-flex-align" v-if="item.leader">
              <em class="xw-color-13 far fa-user"></em>
              <div class="xw-flex-W1">{{item.leader}}</div>
            </div>
            <div class="LRG-label xw-flex xw-flex-align">
              <em class="xw-color-13 far fa-phone-square"></em>
              <div class="xw-flex-W1">{{item.telphone}}</div>
            </div>
            <div class="LRG-abso xw-btn xw-WBlue" @click="getAuditFun(1, item)">联系商友</div>
          </li>
        </ul>
      </div>
    </div>
    <layer-view ref="layer"/>
  </div>
</template>

<style scoped>
/* ---- */
.list-reg li { position: relative; margin: 1rem 1rem 0; padding: 1rem; overflow: hidden; }
.LRG-title { margin: 0 0 0.5rem; line-height: 2.5rem; font-size: 1.7rem; font-weight: bold; }
.LRG-label { line-height: 2.5rem; }
.LRG-label em { margin-right: 1rem; font-size: 1.6rem; }
.LRG-abso { position: absolute; right: 1rem; bottom: 1rem; }
</style>
